package org.ali.creationalpattern.prototypepattern;

import org.ali.creationalpattern.prototypepattern.color.Color;
import org.ali.creationalpattern.prototypepattern.color.Green;
import org.ali.creationalpattern.prototypepattern.color.Red;

import java.util.Hashtable;

/**
 * 缓存
 */
public class ColorCache {
    private static final Hashtable<String, Color> colorMap = new Hashtable<>();

    public static Color getColor(String type) {
        Color color = colorMap.get(type);
        if (color == null) {
            switch (type) {
                case "Red":
                    Color red = new Red("Red");
                    colorMap.put("Red", red);
                    return red;
                case "Green":
                    Color green = new Green("Green");
                    colorMap.put("Green", green);
                    return green;
                default:
                    return null;
            }
        }

        return (Color) color.clone();
    }
}
